package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysUser;
import cn.stylefeng.guns.modular.yingbao.entity.UserBank;
import  cn.stylefeng.guns.modular.yingbao.entity.UserPay;
import cn.stylefeng.guns.modular.yingbao.mapper.SysUserMapper;
import cn.stylefeng.guns.modular.yingbao.mapper.UserBankMapper;
import  cn.stylefeng.guns.modular.yingbao.mapper.UserPayMapper;
import  cn.stylefeng.guns.modular.yingbao.model.params.UserPayParam;
import  cn.stylefeng.guns.modular.yingbao.model.result.UserPayResult;
import   cn.stylefeng.guns.modular.yingbao.service.UserPayService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhyd.oauth.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 用户收款信息表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2019-12-12
 */
@Service
public class UserPayServiceImpl extends ServiceImpl<UserPayMapper, UserPay> implements UserPayService {

    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    UserBankMapper userBankMapper;

    @Override
    public void add(UserPayParam param){
        UserPay entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(UserPayParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(UserPayParam param){
        UserPay oldEntity = getOldEntity(param);
        UserPay newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserPayResult findBySpec(UserPayParam param){
        return null;
    }

    @Override
    public List<UserPayResult> findListBySpec(UserPayParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserPayParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData queryByName(String userid) {
        Integer w = 0;
        Integer z = 0;
        Integer y = 0;
        List<UserPay> userPays = this.baseMapper.selectList(new QueryWrapper<UserPay>().eq("userid", userid));
        if(userPays != null && userPays.size() > 0){
            for(UserPay user : userPays){
                if(user.getName().equals("微信")){
                    w++;
                }else if(user.getName().equals("支付宝")){
                    z++;
                }else if(user.getName().equals("银行卡")){
                    y++;
                }
            }
            return ResponseData.success(200,"顺序为微信支付宝银行卡 0,未开通.1,已开通",w+","+z+","+y);
        }
        return ResponseData.error(500,"都可创建");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData addPay(UserPay userPay) {
        if(userPay.getUserid() != null ) {
            UserPay userPay1 = this.baseMapper.selectOne(new QueryWrapper<UserPay>().eq("userid", userPay.getUserid()).eq("name", userPay.getName()));
            if(userPay1 != null ){
                /*return ResponseData.error(500,"您已创建"+ userPay.getName() + "方式");*/
               if(userPay1.getPayid() != 3){
                   userPay1.setAccount(userPay.getAccount());
                   userPay1.setPic(userPay.getPic());
                   userPay1.setPhone(userPay.getPhone());
                   userPay1.setName(userPay.getName());
                   userPay1.setUsername(userPay.getUsername());
               }else{
                   userPay1.setAccount(userPay.getAccount());
                   userPay1.setPic(userPay.getPic());
                   userPay1.setPhone(userPay.getPhone());
                   userPay1.setBankid(userPay.getBankid());
                   userPay1.setName(userPay.getName());
                   userPay1.setUsername(userPay.getUsername());
                   UserBank userBank = userBankMapper.selectById(userPay.getBankid());
                   if(userBank == null){
                       return ResponseData.error("暂无当前开户行");
                   }
                   userPay1.setBank(userBank.getName());
               }
                int i = this.baseMapper.updateById(userPay1);
                return i > 0 ? ResponseData.success() : ResponseData.error("修改错误");
            }


            userPay.setStatus("0");
            userPay.setVersion(0);
            if(StringUtils.isEmpty(userPay.getName())){
                return ResponseData.error("参数错误");
            }
            if ("微信".equals(userPay.getName())) {
                userPay.setPayid(1);
            } else if ("支付宝".equals(userPay.getName())) {
                userPay.setPayid(2);
            } else if ("银行卡".equals(userPay.getName())) {
                userPay.setPayid(3);
            }else{
                return ResponseData.error("参数错误");
            }
            userPay.setBackup1("0");
            userPay.setBackup2("0");
            userPay.setBackup3(0);
            userPay.setBackup4(0);
            if (StringUtils.isNotEmpty(userPay.getBankid())) {
                UserBank userBank = userBankMapper.selectById(userPay.getBankid());
                userPay.setBank(userBank.getName());
            }
            int insert = this.baseMapper.insert(userPay);

            if (insert > 0) {
                List<UserPay> userid = this.baseMapper.selectList(new QueryWrapper<UserPay>().eq("userid", userPay.getUserid()));
                if(userid.size() > 0){
                    SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("id", userPay.getUserid()));
                    long version = sysUser.getVersion();
                    sysUser.setVersion(sysUser.getVersion()+1);
                    sysUser.setBackup1("1");
                    int update = sysUserMapper.update(sysUser, new QueryWrapper<SysUser>().eq("id", sysUser.getId()).eq("version", version));
                    if(update > 0) {
                        return ResponseData.success(200, "添加" + userPay.getName() + "收款成功", null);
                    }
                    throw new UnsupportedOperationException("失败");
                }
                return ResponseData.success();
            }
        }
        return ResponseData.error(500, "收款信息添加失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData delPayid(String id) {
        UserPay userPay = this.baseMapper.selectById(id);
        int i = this.baseMapper.deleteById(id);
        if(i > 0){
            List<UserPay> userid = this.baseMapper.selectList(new QueryWrapper<UserPay>().eq("userid", userPay.getUserid()));
            if(userid.size() > 0) {
                return ResponseData.success(200, "删除成功", null);
            }
            SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("id", userPay.getUserid()));
            sysUser.setBackup1("0");
            long version = sysUser.getVersion();
            sysUser.setVersion(sysUser.getVersion()+1);
            int update = sysUserMapper.update(sysUser, new QueryWrapper<SysUser>().eq("id", sysUser.getId()).eq("version", version));
            if(update > 0){
                return ResponseData.success();
            }
            throw new UnsupportedOperationException("失败");
        }
        return ResponseData.error(500,"删除失败");
    }

    @Override
    public ResponseData upUserPay(UserPay userPay) {
        int i = this.baseMapper.updateById(userPay);
        if(i > 0){
            return ResponseData.success(200,"修改成功",null);
        }
        return ResponseData.error(500,"修改失败");
    }

    @Override
    public ResponseData queryByUserId(String userid) {
        List<UserPay> userid1 = this.baseMapper.selectList(new QueryWrapper<UserPay>().eq("userid", userid));
        if(userid1 != null && userid1.size() > 0){
            return ResponseData.success(200,"bingo",userid1);
        }
        return ResponseData.error(500,"暂无收账信息");
    }

    @Override
    public ResponseData queryById(Long id) {
        UserPay userPay = this.baseMapper.selectById(id);
        if(userPay != null){
            return ResponseData.success(200,"bingo",userPay);
        }
        return ResponseData.error(500,"查询失败");
    }

    @Override
    public ResponseData queryByUserIdAndName(String userid, String name) {
        UserPay userPay = this.baseMapper.selectOne(new QueryWrapper<UserPay>().eq("userid", userid).eq("payid",name));
        if(userPay != null){
            return ResponseData.success(200,"已有记录",userPay);
        }
        return ResponseData.error(500,"没有啊");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData upStatus(String id) {
        UserPay userPay = this.baseMapper.selectById(id);
        userPay.setStatus("1");
        Integer version = userPay.getVersion();

        userPay.setVersion(userPay.getVersion()+1);
        int i = this.baseMapper.update(userPay,new QueryWrapper<UserPay>().eq("id",id).eq("version",version));
        if(!StringUtils.isEmpty(userPay.getName()) && "银行卡".equals(userPay.getName())){
            SysUser sysUser = this.sysUserMapper.selectById(userPay.getUserid());
            sysUser.setBackup1("1");
            i += this.sysUserMapper.updateById(sysUser);
        }
        if(i > 0){
            return ResponseData.success(200,"修改成功",null);
        }
        return ResponseData.error(500,"修改失败");

    }

    @Override
    public List<UserPay> queryAll1(Long userid) {
        List<UserPay> payList = this.baseMapper.selectList(new QueryWrapper<UserPay>().eq("userid",userid));
        payList.forEach(m -> {
            m.setBackup1(m.getBank());
        });
        if(payList.size() > 0){
            return payList;
        }
        return null;
    }

    private Serializable getKey(UserPayParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserPay getOldEntity(UserPayParam param) {
        return this.getById(getKey(param));
    }

    private UserPay getEntity(UserPayParam param) {
        UserPay entity = new UserPay();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
